﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.SqlClient;

namespace Web2014112110
{
    public partial class Login1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "登录界面";
            //if (Session["CheckCode"] != null)
            //{
            //    string checkcode = Session["CheckCode"].ToString();
            //    if (this.txtSure.Text == checkcode)
            //    {
            //        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('验证码输入正确!')", true);
            //    }
            //    else
            //    {
            //        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('验证码输入错误!')", true);
            //        //return;
            //    }
            //}
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["Theme"] == null)
            {
                Page.Theme = "ThemeFour";
            }
            else if (Session["Theme"].ToString() == "themeFive")
            {
                Page.Theme = "themeFive";
            }
            else if (Session["Theme"].ToString() == "themeTwo")
            {
                Page.Theme = "ThemeTwo";
            }
            else if (Session["Theme"].ToString() == "themeThree")
            {
                Page.Theme = "themeThree";
            }
            else if (Session["Theme"].ToString() == "themeFour")
            {
                Page.Theme = "themeFour";
            }
        }
        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            Session["name"] = this.txtUserName.Text;
            Session["password"] = this.txtPassword.Text;
            string strCn = "server=pc.hstc.cn;database=dhd2014112110;uid=2014112110;password=19941225";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                if (this.RadioButton1.Checked)
                {
                    cn.Open();
                    string strSql = "SELECT * FROM TeaInfo where (Account =N'" + this.txtUserName.Text + "'and Password=N'" + this.txtPassword.Text + "')";
                    SqlCommand cmd = new SqlCommand(strSql, cn);
                    SqlDataReader c = cmd.ExecuteReader();
                    //string password = "";
                    if (c.Read())
                    {
                        //password = (string)c.GetValue(1);
                        string checkcode = Session["CheckCode"].ToString();
                        if (this.txtSure.Text == checkcode)
                        {
                            //Response.Write("<script>javascript:alert('欢迎您的到来');history.back();</script> ");
                            Response.Redirect("TeacherMain.aspx");
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('验证码输入错误！')", true);
                            //Response.Write("<script>javascript:alert('密码错误');history.back();</script>");
                        }

                    }
                    else
                    {
                        Response.Write("<script>javascript:alert('用户名或者密码无效，请重试！');history.back();</script>");
                    }
                    c.Close();
                }
                if (this.RadioButton2.Checked)
                {
                    Session["name"] = this.txtUserName.Text;
                    Session["password"] = this.txtPassword.Text;
                    cn.Open();
                    string strSql = "SELECT * FROM Student where (No =N'" + this.txtUserName.Text + "' and Password=N'" + this.txtPassword.Text + "')";
                    SqlCommand cmd = new SqlCommand(strSql, cn);
                    SqlDataReader c = cmd.ExecuteReader();
                    //string password = "";
                    if (c.Read())
                    {
                        //password = (string)c.GetValue(1);
                        string checkcode = Session["CheckCode"].ToString();
                        if ( this.txtSure.Text == checkcode)
                        {
                            //Response.Write("<script>javascript:alert('欢迎您的到来');history.back();</script> ");
                            Response.Redirect("StuMain.aspx");
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(),"","alert('验证码输入错误！')",true);
                            //Response.Write("<script>javascript:alert('密码错误');history.back();</script>");
                        }

                    }
                    else
                    {
                        Response.Write("<script>javascript:alert('用户名、密码或者用户类型有误，请重试！');history.back();</script>");
                    }
                    c.Close();

                }
            }
            catch
            {
                Response.Write("<script>javascript:alert('连接失败');history.back();</script>");
            }
            finally
            {
                cn.Close();
            }
        }
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112122;uid=2014112122;pwd=19950123";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                string peo = Request.QueryString["peo"];
                if (this.RadioButton1.Checked)
                {
                    Response.Redirect("RegisterOne.aspx");
                }
                if (this.RadioButton2.Checked)
                {
                    Response.Write("<script>alert('对不起，学生没有注册功能，单击“确定”可直接返回。');location.href='index.aspx';</script>");
                }
                else
                {
                    this.lblInfo.Text = "若要注册请选择用户类型";
                }
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
        }
    }
}